/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 复选框视图
    /// </summary>
    public class FCRadioButton : FCCheckBox {
        /// <summary>
        /// 创建复选框
        /// </summary>
        public FCRadioButton() {
        }

        public String groupName = "";

        /// <summary>
        /// 获取组名
        /// </summary>
        public virtual String getGroupName() {
            return groupName;
        }

        /// <summary>
        /// 设置组名
        /// </summary>
        public virtual void setGroupName(String value) {
            groupName = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "RadioButton";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            if (name == "groupname") {
                type = "text";
                value = getGroupName();
            } else {
                base.getAttribute(name, ref value, ref type);
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "GroupName" });
            return attributeNames;
        }

        /// <summary>
        /// 点击方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo) {
            if (!isChecked()) {
                setChecked(!isChecked());
            }
            callTouchEvents("onclick", touchInfo);
            invalidate();
        }

        /// <summary>
        /// 重绘选中按钮方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintCheckButton(FCPaint paint, FCRect clipRect) {
            //绘制背景图
            String bkImage = getPaintingBackImage();
            if (bkImage != null && bkImage.Length > 0) {
                paint.drawImage(bkImage, clipRect);
            } else {
                if (isChecked()) {
                    FCRect innerRect = new FCRect(clipRect.left + 2, clipRect.top + 2, clipRect.right - 3, clipRect.bottom - 3);
                    if (clipRect.right - clipRect.left < 4 || clipRect.bottom - clipRect.top < 4) {
                        innerRect = clipRect;
                    }
                    paint.fillEllipse(getPaintingButtonBackColor(), innerRect);
                }
                paint.drawEllipse(getPaintingButtonBorderColor(), 1, 0, clipRect);
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value) {
            if (name == "groupname") {
                setGroupName(value);
            } else {
                base.setAttribute(name, value);
            }
        }

        /// <summary>
        /// 更新布局方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                if (isChecked())
                {
                    ArrayList<FCView> views = null;
                    if (getParent() != null)
                    {
                        views = getParent().getViews();
                    }
                    else
                    {
                        views = getNative().getViews();
                    }
                    //反选组别相同的项
                    int viewSize = views.size();
                    for (int i = 0; i < viewSize; i++)
                    {
                        FCRadioButton radioButton = views.get(i) as FCRadioButton;
                        if (radioButton != null && radioButton != this)
                        {
                            if (radioButton.getGroupName() == getGroupName() && radioButton.isChecked() == true)
                            {
                                radioButton.setChecked(false);
                                radioButton.invalidate();
                            }
                        }
                    }
                }
            }
        }
    }
}
